package com.meilingcloud.activiti.controller;

import com.meilingcloud.activiti.domain.dto.ProcessDefXmlDto;
import com.meilingcloud.activiti.domain.dto.ProcessDefinitionDto;
import com.meilingcloud.activiti.service.IProcessDefinitionService;
import com.meilingcloud.common.annotation.Log;
import com.meilingcloud.common.core.controller.BaseController;
import com.meilingcloud.common.core.domain.AjaxResult;
import com.meilingcloud.common.core.page.PageDomain;
import com.meilingcloud.common.core.page.TableDataInfo;
import com.meilingcloud.common.core.page.TableSupport;
import com.meilingcloud.common.enums.BusinessType;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/processDefinition")
public class ProcessDefinitionController extends BaseController {

    private final IProcessDefinitionService processDefinitionService;

    /**
     * 获取流程定义列表
     */
    @PreAuthorize("@ss.hasPermi('activiti:definition:list')")
    @GetMapping("/list")
    public TableDataInfo list(ProcessDefinitionDto processDefinitionDto) {
        PageDomain pageDomain = TableSupport.buildPageRequest();
        return getDataTable(processDefinitionService.selectProcessDefinitionList(processDefinitionDto, pageDomain));
    }

    /**
     * 获取流程定义详情
     */
    @GetMapping("/getDefinitions/{instanceId}")
    public AjaxResult getDefinitionByInstanceId(@PathVariable("instanceId") String instanceId) {
        return AjaxResult.success(processDefinitionService.getDefinitionsByInstanceId(instanceId));
    }

    /**
     * 删除流程定义
     */
    @PreAuthorize("@ss.hasPermi('activiti:definition:remove')")
    @Log(title = "流程定义 ", businessType = BusinessType.DELETE)
    @DeleteMapping("/remove/{deploymentId}")
    public AjaxResult delDefinition(@PathVariable("deploymentId") String deploymentId) {
        return AjaxResult.success(processDefinitionService.deleteProcessDefinitionById(deploymentId));
    }

    /**
     * 上传并部署流程定义
     */
    @PreAuthorize("@ss.hasPermi('activiti:definition:add')")
    @Log(title = "流程定义 ", businessType = BusinessType.IMPORT)
    @PostMapping("/uploadStreamAndDeployment")
    public AjaxResult uploadStreamAndDeployment(@RequestParam("file") MultipartFile file) throws IOException {
        processDefinitionService.uploadStreamAndDeployment(file);
        return AjaxResult.success();
    }

    /**
     * 启动/挂起流程定义
     */
    @PreAuthorize("@ss.hasPermi('activiti:definition:edit')")
    @Log(title = "流程定义 ", businessType = BusinessType.UPDATE)
    @PostMapping("/suspendOrActiveApply")
    public AjaxResult suspendOrActiveApply(@RequestBody ProcessDefinitionDto processDefinitionDto) {
        processDefinitionService.suspendOrActiveApply(processDefinitionDto.getId(), processDefinitionDto.getSuspendState());
        return AjaxResult.success();
    }

    /**
     * 上传流程定义
     */
    @PreAuthorize("@ss.hasPermi('activiti:definition:add')")
    @Log(title = "流程定义 ", businessType = BusinessType.IMPORT)
    @PostMapping("/upload")
    public AjaxResult upload(@RequestParam("processFile") MultipartFile file) throws IOException {
        if (!file.isEmpty()) {
            String fileName = processDefinitionService.upload(file);
            return AjaxResult.success("操作成功", fileName);
        }
        return AjaxResult.error("不允许上传空文件！");
    }

    /**
     * 通过bpmn添加流程定义
     */
    @PreAuthorize("@ss.hasPermi('activiti:definition:add')")
    @Log(title = "流程定义 ", businessType = BusinessType.IMPORT)
    @PostMapping("/addDeploymentByBpmn")
    public AjaxResult addDeploymentByBpmn(@RequestBody ProcessDefXmlDto defXmlDto) {
        processDefinitionService.addDeploymentByBpmn(defXmlDto.getBpmnStr());
        return AjaxResult.success();
    }

    /**
     * 获取流程定义XML
     */
    @GetMapping("/getDefinitionXML")
    public void getProcessDefineXML(HttpServletResponse response,
                                    @RequestParam("deploymentId") String deploymentId,
                                    @RequestParam("resourceName") String resourceName) throws IOException {
        processDefinitionService.getProcessDefineXML(response, deploymentId, resourceName);
    }

}
